%%%% Compare the inductor value requirement for buck converter
%%%% and the SC enhanced regulator.
    % inductor reduction ratio, comes from 3 components.
    % D_ratio: less extreme duty ratio
    % f_ratio: increased switching frequency for same switching loss
    % R_ratio: assuming same overal conduction loss
%%%% assumption: 1. operating at the boundary of ccm and dcm  
close all;
clc;
clear;
%% First only consider the effect of duty ratio
figure;

Vout=1;          % unity output voltage
Vin=2:0.1:20;    % input voltage sweep
L=1;            % unity inductance for buck converter
Rload=1;       % Load resistance
Iout=Vout/Rload;
cc=hsv(7);      % color map

% sweep the conversion ratio, N_sc
for N_sc=2:1:8
    D=N_sc*Vout./Vin;      % find the duty ratio for SC enhanced regulator
    D_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin);

    L_reduction_ratio=D_ratio;
    index=find(Vin >= N_sc);
    plot(Vin(index:end),L_reduction_ratio(index:end),'color',cc(N_sc-1,:),'LineWidth',1.5);
    hold on;
end
legend('N=2','N=3','N=4','N=5','N=6','N=7','N=8','Location','NorthWest')
xlabel('Conversion ratio, V_{in}/V_{out}')
ylabel('Normalized inductance')
% ylim([0 1])
% xlim([2 40])
grid on;
% break
%% Consider both duty ratio and FSL performance using Seeman's method
figure;

Vout=1;          % unity output voltage
Vin=2:0.1:20;    % input voltage sweep
L=1;            % unity inductance for buck converter
Rload=1;       % Load resistance
Iout=Vout/Rload;
cc=hsv(7);      % color map

% sweep the conversion ratio, N_sc
for N_sc=2:1:8
    N=Vin./Vout;            % Actual conversion ratio
    D=N_sc*Vout./Vin;      % find the duty ratio for SC enhanced regulator
    M_FSL_buck=1./(N.*(sqrt(N-1)+1).^2);
    M_FSL_dickson=N.^2./(32*(N-1).^2);       % M_FSL for dickson SC only
    M_FSL_dickson_estimate=N_sc.^2/(32*(N_sc-1).^2)./(N./N_sc).^2;
    M_FSL_dickson_estimate2=((N_sc.^2/(32*(N_sc-1).^2)./(N./N_sc).^2).^-1+ ...
    (1./(N/N_sc.*(sqrt(N/N_sc-1)+1).^2)).^-1).^-1;   
%     M_FSL_dickson_buck=M_FSL_dickson_e
    f_ratio=M_FSL_dickson_estimate2./M_FSL_buck;
    D_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin);
%     D_ratio=1;
    L_reduction_ratio=D_ratio./f_ratio;
%     plot(Vin,L_reduction_ratio,'LineWidth',1.5);
    index=find(Vin >= N_sc);
    plot(Vin(index:end),L_reduction_ratio(index:end),'color',cc(N_sc-1,:),'LineWidth',1.5);
    hold on;
end
legend('N=2','N=3','N=4','N=5','N=6','N=7','N=8','Location','NorthWest')
xlabel('Conversion ratio, V_{in}/V_{out}')
ylabel('Normalized inductance')
% ylim([0 1])
% xlim([2 40])
grid on;
break
%% Consider both duty ratio and FSL performance
figure;

Vout=1;          % unity output voltage
Vin=2:0.1:20;    % input voltage sweep
L=1;            % unity inductance for buck converter
Rload=1;       % Load resistance
Iout=Vout/Rload;
cc=hsv(7);      % color map

% sweep the conversion ratio, N_sc
for N_sc=2:1:8
    D=N_sc*Vout./Vin;      % find the duty ratio for SC enhanced regulator
    R_ratio=(D*(2/N_sc+(N_sc-2)^2/N_sc^2)+(1-D)*2);
    f_ratio=2*N_sc^2*2/(6+(N_sc-2)*4)./R_ratio;
    D_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin);

    L_reduction_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin)./f_ratio;
%     plot(Vin,L_reduction_ratio,'LineWidth',1.5);
    index=find(Vin >= N_sc);
    plot(Vin(index:end),L_reduction_ratio(index:end),'color',cc(N_sc-1,:),'LineWidth',1.5);
    hold on;
end
legend('N=2','N=3','N=4','N=5','N=6','N=7','N=8','Location','NorthWest')
xlabel('Conversion ratio, V_{in}/V_{out}')
ylabel('Normalized inductance')
% ylim([0 1])
% xlim([2 40])
grid on;


%% Include the volume of the capacitors
figure;
kvc=1;
kvl=kvc*1000;
fsw=100e3;          % switching frequency
L_buck=(Vin-Vout)/(2*Iout*fsw);
VolumeL=kvl*0.5.*L_buck*Iout^2;

% for different SC conversion ratio
for N_sc=2:1:8
    
    D=N_sc*Vout./Vin;      % find the duty ratio for SC enhanced regulator
    R_ratio=(D*(2/N_sc+(N_sc-2)^2/N_sc^2)+(1-D)*2);
    f_ratio=2*N_sc^2*2/(6+(N_sc-2)*4)./R_ratio;
    D_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin);
    L_reduction_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin)./f_ratio;

    % include the effect of the capacitors
    kc=0;
    for i=1:(N_sc-1)
        kc=kc+i^2;
    end
    L_SC=L_buck.*L_reduction_ratio
    C=(1/(fsw*2*pi))^2./(L_SC);
    index=find(C>100*L_SC)
    C(index)=100*L_SC(index)
    if C>100*(L_buck.*L_reduction_ratio)
        C=100*(L_buck.*L_reduction_ratio)
    end
    VolumeC=kvc*kc*0.5.*C.*(Vin/N_sc).^2;
    L_reduction_ratio_with_cap=L_reduction_ratio+VolumeC./VolumeL;

    index=find(Vin >= N_sc);
    plot(Vin(index:end),L_reduction_ratio_with_cap(index:end),'-','color',cc(N_sc-1,:),'LineWidth',1.5);
    hold on;
    ratio=VolumeC./VolumeL;
%     plot(Vin(index:end),ratio(index:end),'-x','color',cc(N_sc-1,:),'LineWidth',1.5);
end
legend('N=2','N=3','N=4','N=5','N=6','N=7','N=8','Location','NorthWest')
xlabel('Conversion ratio, V_{in}/V_{out}')
ylabel('Normalized Volume')
% ylim([0 1])
% xlim([2 40])
grid on;